package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;

import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.naming.ldap.PagedResultsControl;
import java.text.DecimalFormat;
import java.util.List;

/**
 * ClassName: GoodsServiceImpl
 * Description:
 * Date: 2022/1/15 10:35
 *
 * @version:
 * @since JDK 1.8
 */
@Service
public class GoodsServiceImpl implements GoodsService {



    @Autowired
    private GoodsDao goodsDao;




    @Override
    public Page<Goods> selectPage(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {


        PageHelper.startPage(page,rows);
        Page<Goods> goodsPage =  goodsDao.findPage(codeOrName,goodsTypeId);

        return goodsPage;
    }


    @Override
    public Page<Goods> getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        PageHelper.startPage(page,rows);
        Page<Goods> goodsPage =  goodsDao.findPage(goodsName,goodsTypeId);

        return goodsPage;
    }



    @Override
    public void deleteGoodsByIds(Integer ids) {



        goodsDao.deleteGoodsById(ids);



    }


    @Override
    public void insertOrUpdateGoods(Goods goods) {

        if(goods.getGoodsId() != null){
            Goods goods1 = goodsDao.selectGoodsById(goods.getGoodsId());
            goods.setInventoryQuantity(goods1.getInventoryQuantity());
            goods.setMinNum(goods1.getMinNum());
            goods.setState(goods1.getState());
            goods.setRemarks(goods1.getRemarks());
            goods.setLastPurchasingPrice(goods1.getLastPurchasingPrice());
            goodsDao.UpdateGoodsById(goods);
        }else{
            Integer maxGoodCode = goodsDao.maxGoodCode();

            DecimalFormat df = new DecimalFormat("0000");


            goods.setGoodsCode(df.format(maxGoodCode + 1));







            goodsDao.insertGoods(goods);

        }

    }


    @Override
    public Page<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page,rows);
        Page<Goods> goodsPage =  goodsDao.getNoInventoryQuantity(nameOrCode);

        return goodsPage;
    }


    @Override
    public Page<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        PageHelper.startPage(page,rows);
        Page<Goods> goodsPage =  goodsDao.getHasInventoryQuantity(nameOrCode);

        return goodsPage;
    }


    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.saveStock(goodsId, inventoryQuantity,purchasingPrice);
    }


    @Override
    public ServiceVO deleteStock(Integer goodsId) {

        Goods goods = goodsDao.selectGoodsById(goodsId);
        if(goods.getState() == 1){
            return new ServiceVO(102,"有进货和销售单据的不能删除");
        }

        goodsDao.saveStock(goodsId,0,goods.getPurchasingPrice());
        return new ServiceVO(102,"请求成功");
    }


    @Override
    public List<Goods> getlistAlarm() {
        List<Goods> goodsList = goodsDao.getlistAlarm();
        return goodsList;
    }
}
